package com.tul.exercise.base.clazz.base.eg6;

import com.tul.exercise.base.clazz.base.eg6.base.Pet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import static com.tul.exercise.base.utils.Print.*;

/**
 * @ClassName PetCreator
 * @Description TODO
 * @Author tul 2018/10/12
 * @Version 1.0
 **/
public abstract class PetCreator {
    private Random rand = new Random(47);

    public abstract List<Class<? extends Pet>> types();

    /**
     * 随机实例化对象
     * @return Pet对象
     */
    public Pet randomPet() {
        Pet result = null;
        int n = rand.nextInt(types().size());
        try {
            result = types().get(n).newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            print("实例化对象异常，class=" + types().get(n).getSimpleName());
        }
        return result;
    }

    public Pet[] createArray(int size) {
        Pet[] result = new Pet[size];

        for(int i=0;i<size;i++) {
            result[i] = randomPet();
        }
        return result;
    }

    public ArrayList<Pet> arraylist(int size) {
        ArrayList<Pet> result = new ArrayList<Pet>();
        Collections.addAll(result, createArray(size));
        return result;
    }
}
